package com.demo2do.fms.admin.web.controller.supplier;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.fms.admin.service.supplier.BlackWhiteListService;
import com.demo2do.fms.admin.service.supplier.DriverService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.driver.Driver;
import com.demo2do.fms.entity.driver.EsbBlackWhiteList;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/drivers")
@MenuSetting("menu-supplier")
public class DriverProfileEditController {
	
	private static final Logger logger = LoggerFactory.getLogger(DriverProfileEditController.class);
    
    @Autowired
    private DriverService driverService;
    
    @Autowired
    private BlackWhiteListService blackWhiteListService;
    
    /**
     * 
     * @param id
     * @param driverMenuType
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-{driverMenuType}-driver-management")
    public ModelAndView view(@PathVariable("id") Long id, @RequestParam("driverMenuType") String driverMenuType) {

        ModelAndView modelAndView = new ModelAndView("supplier/management/driver/profile/driver-view");
        
        Driver driver = driverService.load(id);
        
        modelAndView.addObject("driver", driver);
        
        modelAndView.addObject("driverMenuType", driverMenuType);
        
        List<EsbBlackWhiteList> esbBlackWhiteLists = blackWhiteListService.listByCitizenCode(driver.getCitizenCode());
        
        modelAndView.addObject("esbBlackWhiteLists", esbBlackWhiteLists);

        return modelAndView;
        
    }
    
    /**
     * 修改司机基本信息 - panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit-basic", method = RequestMethod.GET)
    public ModelAndView editBasic(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("supplier/management/driver/profile/driver-basic-edit");

        Driver driver = driverService.load(id);
        modelAndView.addObject("driver", driver);
        
        return modelAndView;
        
    }
    
    /**
     * 修改司机基本信息
     * 
     * @param driver
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}/edit-basic", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditBasic(Driver driver, @Secure(property = "id") Long operator) {
    	
    	HttpResult httpResult = new HttpResult();
    	
    	try {

        	// edit info
            driverService.editBasic(driver, operator);
            
            httpResult.success().message("修改成功");
        	
		} catch (Exception e) {
			logger.error("#onEditBasic() - edit fail, exception is [{}]", e);
			httpResult.fail("500").message("修改失败! " + e.getMessage());
		}
    	
    	return httpResult;
    	
    }
    
    /**
     * 修改司机个人信息 - panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit-profile", method = RequestMethod.GET)
    public ModelAndView editProfile(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("supplier/management/driver/profile/driver-profile-edit");

        Driver driver = driverService.load(id);
        modelAndView.addObject("driver", driver);
        
        return modelAndView;
        
    }
    
    /**
     * 修改司机个人信息
     * 
     * @param driver
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}/edit-profile", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditProfile(@Preloading("driverId") Driver driver,
				                                  @Secure(property = "id") Long operator) {
        
    	HttpResult httpResult = new HttpResult();
    	
        try {

        	// edit info
        	Date now = new Date();
            driverService.editProfile(driver.operate(now, operator));
            
            httpResult.success().message("修改成功");
        	
		} catch (Exception e) {
			logger.error("#onEditProfile() - edit profile fail, exception = [{}]", e);
			httpResult.fail("500").message("修改失败");
		}
        
        return httpResult;
        
    }
    
    /**
     * 上传司机证件信息
     * 
     * @param id
     * @param type
     * @param driverMenuType
     * @param fileObject
     * @param objectId
     * @param modal
     * @param acceptType
     * @return
     */
    @RequestMapping(value = "/{id}/cover", method = RequestMethod.GET)
    public ModelAndView editCover(@PathVariable("id") Long id,
                                  @RequestParam("type") String type,
                                  @RequestParam("driverMenuType") String driverMenuType,
                                  @RequestParam("fileObject") String fileObject,
                                  @RequestParam("objectId") Long objectId,
                                  @RequestParam(name = "modal", required = false, defaultValue = "false") boolean modal,
                                  @RequestParam(name = "acceptType", required = false) String acceptType){
        
        ModelAndView modelAndView = new ModelAndView("supplier/management/driver/driver-cover-edit");
        
        modelAndView.addObject("driverId", id);
        modelAndView.addObject("type", type);
        modelAndView.addObject("driverMenuType", driverMenuType);
        modelAndView.addObject("fileObject", fileObject);
        modelAndView.addObject("objectId", objectId);
        modelAndView.addObject("modal", modal);
        modelAndView.addObject("acceptType", acceptType);
        
        return modelAndView;
    }

}
